package com.atguigui.leetcode1;

/**
 * 2748. 美丽下标对的数目
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/6/26 9:57
 */
public class P2748NumberOfBeautifulPairs {
    public static void main(String[] args) {
        Solution solution = new P2748NumberOfBeautifulPairs().new Solution();
        int[] nums = {2, 5, 1, 4};
        int i = solution.countBeautifulPairs(nums);
        System.out.println(i);
    }

    class Solution {
        public int countBeautifulPairs(int[] nums) {
            int ans = 0;
            int n = nums.length;
            for (int i = 0; i < n - 1; i++) {
                for (int j = i + 1; j < n; j++) {
                    String a = nums[i] + "";
                    String b = nums[j] + "";
                    int x = Integer.valueOf(a.substring(0, 1));
                    int l = b.length();
                    int y = Integer.valueOf(b.substring(l - 1, l));
                    if (gcd(x, y) == 1) {
                        ans++;
                    }
                }
            }

            return ans;
        }

        // 求两数的最大公因数
        public int gcd(int a, int b) {
            if (b == 0) {
                return a;
            } else {
                return gcd(b, a % b);
            }
        }


    }
}
